import { createRouter, createWebHistory } from 'vue-router';
import store from '../store'
import { Notify } from "vant";

const Home = () => import('views/home/index');
const Category = () => import('views/category/index');
const Detail = () => import('views/detail/index');
const Profile = () => import('views/profile/index');
const Shopcart = () => import('views/shopcart/index');
const Register = () => import('views/profile/register');
const Address = () => import('views/profile/address');
const Addressedit = () => import('views/profile/addressedit')
const Order = () => import('../views/order/order');
const OrderDetail = () => import('../views/order/orderDetail');
const CreateOrder = () => import('../views/order/createOrder');

const routes = [
  {
    path: '',
    name: 'DefaultHome',
    // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    component: Home,
    meta: {
      title: '图书星弟-首页'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      title: '图书星弟-首页'
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: Category,
    meta: {
      title: '图书星弟-商品分类'
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: Detail,
    meta: {
      title: '图书星弟-商品详情'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: {
      title: '图书星弟-个人中心',
      isLoginRequired: true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: {
      title: '图书星弟-注册'
    }
  },
  {
    path: '/shopcart',
    name: 'Shopcart',
    component: Shopcart,
    meta: {
      title: '图书星弟-购物车',
      isLoginRequired: true
    }
  },
  {
    path: '/address',
    name: 'Address',
    component: Address,
    meta: {
      title: '图书星弟-地址管理',
      isLoginRequired: true
    }
  },
  {
    path: '/addressedit',
    name: 'Addressedit',
    component: Addressedit,
    meta: {
      title: '图书星弟-编辑地址',
      isLoginRequired: true
    }
  },
  {
    path: '/order',
    name: 'Order',
    component: Order,
    meta: {
      title: '图书星弟-订单列表',
      isLoginRequired: true
    }
  },
  {
    path: '/createorder',
    name: 'CreateOrder',
    component: CreateOrder,
    meta: {
      title: '图书星弟-生成订单',
      isLoginRequired: true
    }
  },
  {
    path: '/orderdetail',
    name: 'OrderDetail',
    component: OrderDetail,
    meta: {
      title: '图书星弟-订单详情',
      isLoginRequired: true
    }
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, form, next) => {
  if (to.meta.isLoginRequired && !store.getters.getIsLogin) {
    Notify('请登录')
    return next('/register')
  } else {
    next();
  }
  document.title = to.meta.title;
})

export default router
